package cn.wolfcode.web.controller;


import cn.wolfcode.ssm.domain.Customer;
import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.domain.SystemDictionaryItem;
import cn.wolfcode.ssm.query.CustomerQueryObject;
import cn.wolfcode.ssm.service.ICustomerService;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.service.IEmployeeService;
import cn.wolfcode.ssm.service.ISystemDictionaryItemService;
import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.fasterxml.jackson.databind.ser.std.StdKeySerializers;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.util.resources.cldr.teo.CalendarData_teo_KE;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;


    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 给高查的默认状态设置成潜在客户
       // qo.setStatusId(Customer.STATUS_COMMON);

        // 调用shiro的方法
        Subject subject = SecurityUtils.getSubject();
        // 如果没有管理员或者是业务经理的角色.普通人员登陆只能看自己的客户
        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
            // 拿到当前的登陆对象
            Employee employee = (Employee)subject.getPrincipal();
            // 把qo中SellerId 高查条件设置属性值
            qo.setSellerId(employee.getId());
        }


        // 只查询带有市场专员或市场角色的员工
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        // 共享到界面上
        model.addAttribute("employees",employees);


        // 获取职业下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectParenSn("job");
        model.addAttribute("jobs",jobs);

        // 获取客户来源数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectParenSn("source");
        model.addAttribute("sources",sources);

        //  获取客户联系方式 communicationMethod
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectParenSn("communicationMethod");
        model.addAttribute("ccts",ccts);

        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonPage delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonPage();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonPage saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonPage();
    }


    // 第二种方法.传了id和name过来
    @RequestMapping("/checkTel")
    @ResponseBody
    public HashMap checkTel(String tel, Long id){
        // 新增时用传上来的tel去数据库进行比对,看是否有号码相同的员工,如果没有就返回true
        HashMap map = new HashMap<Object,Object>();
        if(id == null) {
            Customer customer = customerService.selectCheckTel(tel);
            map.put("valid", customer == null);
            return map;
        }

        // 编辑时候通过用户传上来的拿到原来的手机号,如果没修改.也返回true
        String OldTel = customerService.selectTelById(id);
        if (OldTel.equals(tel)){
            map.put("valid",true);
            return map;
        }

        // 如果编辑时用户修改了号码,则在验证修改的号码是否和数据库中的号码相同
        Customer customer = customerService.selectCheckTel(tel);
        map.put("valid",customer==null);


        return map;

    }



    @RequestMapping("/UpdateStatus")
    @ResponseBody
    public JsonPage updateStatus(Customer customer, @ModelAttribute("qo") CustomerQueryObject qo){

        customerService.updateStatus(customer.getId(),customer.getStatus());


        return new JsonPage();
    }


    // 数据池客户
    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 给高查的默认状态设置成潜在客户
        qo.setStatusId(Customer.STATUS_POOL);

//        // 调用shiro的方法
//        Subject subject = SecurityUtils.getSubject();
//        // 如果没有管理员或者是业务经理的角色.普通人员登陆只能看自己的客户
//        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
//            // 拿到当前的登陆对象
//            Employee employee = (Employee)subject.getPrincipal();
//            // 把qo中SellerId 高查条件设置属性值
//            qo.setSellerId(employee.getId());
//        }


        // 只查询带有市场专员或市场角色的员工
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        // 共享到界面上
        model.addAttribute("employees",employees);


        // 获取职业下拉框数据 jobs
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectParenSn("job");
        model.addAttribute("jobs",jobs);

        // 获取客户来源数据 sources
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectParenSn("source");
        model.addAttribute("sources",sources);


        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/poolList";
    }




    // 失败客户
    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/failList")
    public String failList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        // 给高查的默认状态设置成潜在客户
        qo.setStatusId(Customer.STATUS_FAIL);

//        // 调用shiro的方法
//        Subject subject = SecurityUtils.getSubject();
//        // 如果没有管理员或者是业务经理的角色.普通人员登陆只能看自己的客户
//        if (!(subject.hasRole("ADMIN")||subject.hasRole("Market_Manager"))){
//            // 拿到当前的登陆对象
//            Employee employee = (Employee)subject.getPrincipal();
//            // 把qo中SellerId 高查条件设置属性值
//            qo.setSellerId(employee.getId());
//        }


        // 只查询带有市场专员或市场角色的员工
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        // 共享到界面上
        model.addAttribute("employees",employees);


        // 获取职业下拉框数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectParenSn("job");
        model.addAttribute("jobs",jobs);

        // 获取客户来源数据
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectParenSn("source");
        model.addAttribute("sources",sources);

        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/failList";
    }



}
